// pages/userhome/address/address.js
var app = getApp();
Page({
  data: {

    choo_modal: "none",
    radioValue: 1,
    checked: true,
    afterSafeList:[],
  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      type: options.type
    })
    that.setData({
     
      balance: wx.getStorageSync("balance")
    })
    console.info(options)
    var orderId = options.orderId;
    that.setData({
      orderId: orderId
    })
    that.onShow();
    
  },

  onReady: function () {
    // 页面渲染完成
  },
  goLogistice: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.info(this.data.expressArray);
    if (this.data.orderProductList[e.currentTarget.dataset.index].expressCompanyCode != undefined) {
      wx.navigateTo({
        url: '/pages/logistics/logistics?expressCompanyCode=' + this.data.orderProductList[e.currentTarget.dataset.index].expressCompanyCode + '&trackingNumber=' + this.data.orderProductList[e.currentTarget.dataset.index].trackingNumber + '&expressCompany=' + this.data.orderProductList[e.currentTarget.dataset.index].expressCompany + '&expressInfo=' + that.data.orderProductList[index].expressInfo + '&thumbnailimage=' + e.currentTarget.dataset.thumbnailimage+ '&telephone=' + that.data.order.contactTelephone
      })
    }
    

  },
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + "getOrderDetails",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        orderId: that.data.orderId
      },
      method: "POST",
      success: function (res) {
        var data = res.data;
        var orderProductList = data.data.orderProductList;
        if (data.result === 1) {
          if (data.data.orderStatus == 7000) {
            that.setData({
              edTime: '2020-02-31 22:33:27'
            })
            that.countTime();
          }
          if (data.data.orderStatus == 7010) {
            wx.request({
              url: app.globalData.url + 'getParaById',
              header: {
                "content-type": "application/x-www-form-urlencoded"
              },
              data: {
                paraId: 'order_product_refund_ audit',
              },
              method: "POST",
              success: function (res) {
                that.setData({
                  addDayCount: res.data.data.value
                })
                var date = new Date();
                var now = date.getTime();
                //console.info(a);
                var edTime = that.getDateStr(null, res.data.data.value);
                that.setData({
                  edTime: edTime
                })
                that.countTime(edTime);
              }
            })
          }
          orderProductList.forEach((item) => {
            if (item.product.bookingConsignmentTime != undefined && item.product.bookingEndTime != undefined) {
              item.product.bookingConsignmentTime = item.product.bookingConsignmentTime.substring(0, 10); //要截取时间的字符串
              item.product.bookingEndTime = item.product.bookingEndTime.substring(0, 10); //要截取时间的字符串
            }
          })
          console.info(orderProductList);
          that.setData({
            order: data.data,
            orderStatus: data.data.orderStatus,
            contactTelephone: data.data.contactTelephone,
            contactAddress: data.data.contactAddress,
            contactUsername: data.data.contactUsername,
            list: data.list,
            orderNumber: data.data.orderNumber,
            orderTime: data.data.orderTime,
            orderNumber: data.data.orderNumber,
            payFinishTime: data.data.payFinishTime,
            updateTime: data.data.updateTime,
            orderTrackingNumber: data.list.orderTrackingNumber,
            orderProductList: data.data.orderProductList,
            expressArray: data.data.expressArray,
            afterSafeList: data.data.afterSafeList,
          })
        }
      }
    });
    
  },
  onUnload: function () {
    // 页面关闭
  },

  navBack: function () {
    wx.navigateBack({

    })
  },
  countTime:function(edTime) {
    //var edTime ='2020-02-21 22:33:27';
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(that.data.edTime.replace(/\.|\-/g, '/'));//设置截止时间
    var end = endDate.getTime();
    var leftTime = end - now; //时间差                              
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
      that.setData({
        countdown: d + "天" + h + "时" + m + "分" + s + "秒",
      })
      //递归每秒调用countTime方法，显示动态时间效果
      setTimeout(that.countTime, 100);
    } else {
      console.log('已截止')
      that.setData({
        countdown: '00:00:00'
      })
    }

  },

  //获取几天后的日期
  getDateStr:function (today, addDayCount) {
    console.info(addDayCount);
    var dd;
    if(today) {
      dd = new Date(today);
    }else{
      dd = new Date();
    }
    dd.setDate(parseInt(dd.getDate()) + parseInt(addDayCount));//获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var month = dd.getMonth() + 1;//获取当前月份的日期 
    var d = dd.getDate();
    var h = dd.getHours();
    var m = dd.getMinutes();
    var s = dd.getSeconds();
    if(m < 10) {
      m = '0' + m;
    };
    if(d < 10) {
      d = '0' + d;
    };
    return y + "-" + month + "-" + d + " " + h + ':' + m + ':' + s;
  }
})